package com.shop2cn.iapi.sqoptrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.ReduceSqProductStocksReq;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ReduceSqProductStocksCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.BargainGroupService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingProductService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 批量接单 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SqReduceProductStocks {

	private static ReduceSqProductStocksReq reduceSqProductStocksReq;

	private static ReduceSqProductStocksCall reduceSqProductStocksCall;
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("扣除神器库存-砍价团使用");
	}

	@Before
	public void caseUp() {

		reduceSqProductStocksReq = new ReduceSqProductStocksReq();
		reduceSqProductStocksCall = new ReduceSqProductStocksCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(reduceSqProductStocksCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ReduceSqProductStocks_001() {
		Logger.start(true, "神器创建砍价团参团订单,砍价团通知组团成功，扣除库存成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			// 砍价团id
			String bizId = TestDataManager.getRandomNum(9);
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, true);

			// 开团
			TradingServiceResp openOrder = BargainGroupService.shenqiOpenGroup(
					bizId, productBean, true);
			
			Thread.sleep(5000);
			
			long openOrderId = openOrder.getOrderList().get(0).getOrderId();
			
			// 扣除开团库存
			reduceSqProductStocksReq.setOrderId(openOrderId);

			reduceSqProductStocksCall.setData(reduceSqProductStocksReq);
			reduceSqProductStocksCall.callService();

			// 参团
			TradingServiceResp joinOrder2 = BargainGroupService
					.shenqiJoinGroup(bizId, productBean, true);
			
			Thread.sleep(5000);

			long orderId = joinOrder2.getOrderList().get(0).getOrderId();
			
			// 扣除参团库存
			reduceSqProductStocksReq.setOrderId(orderId);

			reduceSqProductStocksCall.setData(reduceSqProductStocksReq);
			reduceSqProductStocksCall.callService();
			
			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 2, 3);

			Logger.comment("前置数据准备结束");
			
			SqTradingProductService.getProductDetails(
					productBean.getSellerId(), productBean.getProductId());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_ReduceSqProductStocks_002() {
		Logger.start(true, "神器创建砍价团参团订单,砍价团通知组团成功，扣除库存服务调用失败，发送返库存消息");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			// 砍价团id
			String bizId = TestDataManager.getRandomNum(9);
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, true);

			BargainGroupService.shenqiOpenGroup(bizId, productBean, true);

			// 参团
			TradingServiceResp placeOrder2 = BargainGroupService
					.shenqiJoinGroup(bizId, productBean, true);

			long orderId = placeOrder2.getOrderList().get(0).getOrderId();
			
			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 2, 3);

			Logger.comment("前置数据准备结束");

			// 杀掉神器库存服务
			reduceSqProductStocksReq.setOrderId(orderId);

			reduceSqProductStocksCall.setData(reduceSqProductStocksReq);
			reduceSqProductStocksCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
